﻿<META http-equiv="refresh" content="<?php echo rand(500,600); ?>" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px;}
td{font-size:12px;}
</style>
<?php


/**
 * 国际足球视频数据采集
 */
header('Content-type: text/html; charset=utf-8');

include_once 'module/simple_html_dom.php';

date_default_timezone_set("Asia/Shanghai");
@ $date = $_GET["date"];

if ($date == "") {
	$searchDate = date("Ymd");
	$date = date("Y-m-d");
}

//连接数据库
$config = require 'conf/config.ini.php'; //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);

//先取得当前数据库中的数据
$sql = "select matchTime, videoId from live_video where videoCategory = '3'";
$result = mysqli_query($conn, $sql);

//中超
$url = "http://sports.sina.com.cn/cslvideo/";

$url = file_get_contents($url);
$url = substr($url, strpos($url, "URL=") + 4, strlen($url));
$url = substr($url, 0, strpos($url, ">"));

//$url = "http://sports.sina.com.cn/z/cslvideo12_03/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
$HtmlContents = curl_exec($ch);
curl_close($ch);

$sql = "INSERT INTO live_video (matchTime, videoId, videoTitle, videoImgUrl, videoLength, videoUrl, homeTeam, visitTeam, videoCategory, updateDate) VALUES";

$HtmlContents = str_get_html($HtmlContents);

$teamList = $HtmlContents->find('div[class=T_Menu_01] span');

$i = 0;
$j = 0;
foreach ($teamList as $index => $team) {
	$homeTeam = null;
	$visitTeam = null;
	$teamName = iconv("gbk", "utf-8", $team->innertext);

	$teamName = explode("VS", $teamName);
	//取得主队名称
	$homeTeam = $teamName[0];
	//取得客队名称
	$visitTeam = $teamName[1];

	$videoList = $HtmlContents->find('div[ID=S_Cont_0' . $index . ']', 0);
	$videoList = $videoList->find('div[class=box_video]');
	foreach ($videoList as $video) {

		$matchTime = $video->find('a', 0)->href;
		$matchTime = substr($matchTime, 38, 10);
		echo "matchTime：" . $matchTime;
		echo '<br>';
		$videoTitle = $video->find('h5', 0)->plaintext;
		$videoTitle = iconv("gbk", "utf-8", $videoTitle);
		echo "title：" . $videoTitle;
		echo '<br>';
		echo "homeTeam：" . $homeTeam;
		echo '<br>';
		echo "visitTeam：" . $visitTeam;
		echo '<br>';
		$videoImgUrl = $video->find('a img ', 0)->src;
		echo "imgurl：" . $videoImgUrl;
		echo '<br>';
		$videoId = substr($videoImgUrl, strripos($videoImgUrl, "/") + 1, 10);
		echo "id：" . $videoId;
		echo '<br>';
		echo "videoLength：0";
		echo '<br>';
		$videoUrl = "http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=" . $videoId . "/s.swf";
		echo "videoUrl：" . $videoUrl;

		$insert = true;
		mysqli_data_seek($result, 0);
		while ($row_array = mysqli_fetch_array($result, MYSQL_ASSOC)) {
			if ($row_array["videoId"] == $videoId && $row_array["matchTime"] == $matchTime) {
				$insert = false;
				break;
			}
		}
		if ($insert) {
			$j = $j +1;
			$sql .= "('" . $matchTime . "', '" . $videoId . "', '" . $videoTitle . "', '" . $videoImgUrl . "', '', '" . $videoUrl . "', '" . $homeTeam . "', '" . $visitTeam . "', '3', now()),";
		}

		echo '<hr>';
	}
}
$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";

if ($j > 0) {
	//连接数据库
	$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);
	//插入比赛比分
	mysqli_multi_query($conn, $sql);
}
?>